package cn.tedu.spring.life;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.sql.SQLOutput;

/**
 * Bean对象的生命周期:
 *   1.实例化:无参构造;
 *   2.属性赋值:set方法;
 *   3.初始化: @PostConstruct;
 *   4.开发者使用;
 *   5.销毁: @PreDestroy;
 */
@Component
public class DBConnect {
    private String username;

    //第1步
    public DBConnect(){
        System.out.println("1-调用无参构造实例化");
    }
    //第2步
    @Value("root")
    public void setUsername(){
        this.username = username;
        System.out.println("2-调用set方法属性赋值");
    }
    //第3步
    @PostConstruct
    public void init(){
        System.out.println("3-执行生命周期初始化的方法");
    }
    //第4步
    @PreDestroy
    public void destroy(){
        System.out.println("5-执行生命周期销毁的方法");
    }
}
